package com.pan.admin.module.loanUser.contorller;

import com.pan.admin.common.annotation.Log;
import com.pan.admin.common.core.controller.BaseController;
import com.pan.admin.common.core.domain.AjaxResult;
import com.pan.admin.common.core.page.TableDataInfo;
import com.pan.admin.common.enums.BusinessType;
import com.pan.admin.common.utils.poi.ExcelUtil;
import com.pan.admin.module.crm.entity.vo.CrmDistributeLoanUserVO;
import com.pan.admin.module.loanUser.entity.LoanUserFormField;
import com.pan.admin.module.loanUser.entity.po.LoanUserFormFieldPO;
import com.pan.admin.module.loanUser.entity.vo.LoanUserFormFieldVo;
import com.pan.admin.module.loanUser.service.LoanUserFormFieldService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.List;


/**
 * 用户资质列表Controller
 *
 * @author ts
 * @date 2023-10-11
 */
@RestController
@RequestMapping("/loanUser/loanUserFormField")
public class LoanUserFormFieldController extends BaseController {
    @Autowired
    private LoanUserFormFieldService loanUserFormFieldService;

    /**
     * 查询用户资质列表列表 表头
     */
    @PreAuthorize("@ss.hasPermi('loanUser:loanUserFormField:list')")
    @GetMapping("/list")
    public TableDataInfo list(LoanUserFormField loanUserFormField) {
        List<LoanUserFormField> list = loanUserFormFieldService.list(loanUserFormField);
        return getDataTable(list);
    }

    /**
     * 获取相关联用户资质
     */
    @PreAuthorize("@ss.hasPermi('loanUser:loanUserFormField:field')")
    @GetMapping("/getUserField")
    public AjaxResult getUserField(CrmDistributeLoanUserVO crmDistributeLoanUserVO) {
        // 防止越权
        Integer id = crmDistributeLoanUserVO.getLoanUserId();
        if (id == null) {
            return error("非法获取");
        }
        //  获取用户关联资质
        List<LoanUserFormFieldPO> userFormField = loanUserFormFieldService.getUserFormField(id);
        return success(userFormField);
    }

    /**
     * 查询用户资质列表列表 数据
     * TODO 这里可能导致越权漏洞
     */
    @PreAuthorize("@ss.hasPermi('loanUser:loanUserFormField:userFormFieldList')")
    @GetMapping("/userFormFieldList")
    public TableDataInfo userFormFieldList(LoanUserFormFieldVo loanUserFormField) {
        startPage();
        List<LoanUserFormFieldVo> list = loanUserFormFieldService.userFormFieldList(loanUserFormField);
        return getDataTable(list);
    }

    /**
     * 导出用户资质列表列表
     */
    @PreAuthorize("@ss.hasPermi('loanUser:loanUserFormField:export')")
    @Log(title = "用户资质列表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, LoanUserFormField loanUserFormField) {
        List<LoanUserFormField> list = loanUserFormFieldService.list(loanUserFormField);
        ExcelUtil<LoanUserFormField> util = new ExcelUtil<LoanUserFormField>(LoanUserFormField.class);
        util.exportExcel(response, list, "用户资质列表数据");
    }

    /**
     * 获取用户资质列表详细信息
     * TODO 这里可能导致越权漏洞
     */
    @PreAuthorize("@ss.hasPermi('loanUser:loanUserFormField:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(loanUserFormFieldService.getById(id));
    }


    /**
     * 新增用户资质列表
     */
    @PreAuthorize("@ss.hasPermi('loanUser:loanUserFormField:add')")
    @Log(title = "用户资质列表", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody LoanUserFormField loanUserFormField) {
        return toAjax(loanUserFormFieldService.insertLoanUserFormField(loanUserFormField));
    }

    /**
     * 修改用户资质列表
     */
    @PreAuthorize("@ss.hasPermi('loanUser:loanUserFormField:edit')")
    @Log(title = "用户资质列表", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody LoanUserFormField loanUserFormField) {
        return toAjax(loanUserFormFieldService.updateLoanUserFormField(loanUserFormField));
    }


}
